package controller;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.File;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

/**
 * Servlet implementation class UploadImgServlet
 */
@WebServlet(name= "/UploadImgServlet",urlPatterns="/upload")
public class UploadImgServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
//		request.setCharacterEncoding("GBK");
		SmartUpload smartUpload = new SmartUpload();
		//初始化	
		smartUpload.initialize(this.getServletConfig(), request, response); 	
		smartUpload.setTotalMaxFileSize(100000000);
		smartUpload.setAllowedFilesList("jpg");
		try {
			smartUpload.upload();
			System.out.println("upload success!!");
		} catch (SmartUploadException e1) {
			e1.printStackTrace();
		}
		String methodName = smartUpload.getRequest().getParameter("method");
		System.out.println(methodName);
		
		
		if(methodName.equals("submitImagination9Img")) {
			//得到上传的文件对象
			File smartFile = smartUpload.getFiles().getFile(0);
			System.out.println("得到对象！");
			//保存文件
			String fileName = smartFile.getFileName();
			System.out.println(fileName);
			try {
				smartFile.saveAs("D:/Documents/Documents/ecilpese/eclipse-jee-luna/workspace/phone-introduce/WebContent/assets/img/机型介绍图/" + fileName,
						smartUpload.SAVE_PHYSICAL);
			} catch (SmartUploadException e) {
				e.printStackTrace();
			}
		}
		
		if(methodName.equals("submitMate20Img")) {
			//得到上传的文件对象
			File smartFile = smartUpload.getFiles().getFile(0);
			System.out.println("得到对象！");
			//保存文件
			String fileName = smartFile.getFileName();
			System.out.println(fileName);
			try {
				smartFile.saveAs("D:/Documents/Documents/ecilpese/eclipse-jee-luna/workspace/phone-introduce/WebContent/assets/img/机型介绍图/" + fileName,
						smartUpload.SAVE_PHYSICAL);
			} catch (SmartUploadException e) {
				e.printStackTrace();
			}
		}
		
		if(methodName.equals("submitMate30Img")) {
			//得到上传的文件对象
			File smartFile = smartUpload.getFiles().getFile(0);
			System.out.println("得到对象！");
			//保存文件
			String fileName = smartFile.getFileName();
			System.out.println(fileName);
			try {                                                                                                                                                                       
				smartFile.saveAs("D:/Documents/Documents/ecilpese/eclipse-jee-luna/workspace/phone-introduce/WebContent/assets/img/机型介绍图/" + fileName,
						smartUpload.SAVE_PHYSICAL);
			} catch (SmartUploadException e) {
				e.printStackTrace();
			}
		}
		
		if(methodName.equals("submitP20Img")) {
			//得到上传的文件对象
			File smartFile = smartUpload.getFiles().getFile(0);
			System.out.println("得到对象！");
			//保存文件
			String fileName = smartFile.getFileName();
			System.out.println(fileName);
			try {
				smartFile.saveAs("D:/Documents/Documents/ecilpese/eclipse-jee-luna/workspace/phone-introduce/WebContent/assets/img/机型介绍图/" + fileName,
						smartUpload.SAVE_PHYSICAL);
			} catch (SmartUploadException e) {
				e.printStackTrace();
			}
		}
		
		if(methodName.equals("submitNovaImg")) {
			//得到上传的文件对象
			File smartFile = smartUpload.getFiles().getFile(0);
			System.out.println("得到对象！");
			//保存文件
			String fileName = smartFile.getFileName();
			System.out.println(fileName);
			try {
				smartFile.saveAs("D:/Documents/Documents/ecilpese/eclipse-jee-luna/workspace/phone-introduce/WebContent/assets/img/机型介绍图/" + fileName,
						smartUpload.SAVE_PHYSICAL);
			} catch (SmartUploadException e) {
				e.printStackTrace();
			}
		}
		
		
		if(methodName.equals("submitP30Img")) {
			//得到上传的文件对象
			File smartFile = smartUpload.getFiles().getFile(0);
			System.out.println("得到对象！");
			//保存文件
			String fileName = smartFile.getFileName();
			System.out.println(fileName);
			try {
				smartFile.saveAs("D:/Documents/Documents/ecilpese/eclipse-jee-luna/workspace/phone-introduce/WebContent/assets/img/机型介绍图/" + fileName,
						smartUpload.SAVE_PHYSICAL);
			} catch (SmartUploadException e) {
				e.printStackTrace();
			}
		}
	}

}
